package com.example.carprovideruser.service.impl.hkf;

import com.alibaba.dubbo.config.annotation.Service;
import com.example.carinterface.pojo.BookingApplication;
import com.example.carinterface.pojo.BusinessOrder;
import com.example.carinterface.pojo.OrderState;
import com.example.carinterface.service.hkf.BusinessOrderService;
import com.example.carinterface.utils.ResultJson;
import com.example.carprovideruser.mapper.hkf.BusinessOrderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
public class BusinessOrderServiceImpl implements BusinessOrderService {
    @Autowired
    BusinessOrderMapper businessOrderMapper;
    @Override
    @Transactional
    public ResultJson insertBusinessOrder(BookingApplication bookingApplication, BusinessOrder businessOrder) {
        ResultJson resultJson=new ResultJson();
if (bookingApplication.getCouponId()!=null){
    businessOrderMapper.updateMyCoupon(bookingApplication);
}
if (bookingApplication.getPointPay()!=null){
    businessOrderMapper.updatePointPay(bookingApplication);
}
       if(businessOrderMapper.insertBookingApplication(bookingApplication)>0){
Integer  bookingApplicationId=bookingApplication.getId();
businessOrder.setBookingApplicationId(bookingApplicationId);
businessOrder.setOrderSource(2);
if ( businessOrderMapper.insertBusinessOrder(businessOrder)>0){
    OrderState orderState=new OrderState();
    if(bookingApplication.getOrderState()==1){orderState.setState(2);}else {orderState.setState(1);}

    orderState.setBusinessOrderId(businessOrder.getId());
    if(businessOrderMapper.insertOrderState(orderState)>0){
 return resultJson.setSuccess("下单成功，待处理");
}}}
        return resultJson.setFail("失败");
    }

    @Override
    public ResultJson selectMyBusinessOrderState(Map<String, Object> map) {
        ResultJson resultJson=new ResultJson();
       List<Map> list= businessOrderMapper.selectMyBusinessOrderState(map);
        return resultJson.setSuccess("成功",list);
    }

    @Override
    @Transactional
    public ResultJson updateBusinessOrder(Map<String, Object> map) {
          ResultJson resultJson=new ResultJson();
       if( businessOrderMapper.updateBookingApplication(map)>0){
           if ((Integer)map.get("orderNo")==1){
          OrderState orderState=new OrderState();
          orderState.setState(2);
          orderState.setBusinessOrderId((Integer) map.get("businessOrderId"));
           businessOrderMapper.insertOrderState(orderState);
           return resultJson.setSuccess("支付成功");
       }
       return resultJson.setSuccess("修改成功");}
       return resultJson.setFail("支付失败");
    }

    @Override
    @Transactional
    public ResultJson insertDriverComment(Map<String, Object> map) {
        ResultJson  resultJson=new ResultJson();
        if (businessOrderMapper.insertDriverComment(map)>0){
        OrderState orderState=new OrderState();
        orderState.setBusinessOrderId((Integer)map.get("businessorderId"));
        orderState.setState(14);
      if ( businessOrderMapper.insertOrderState(orderState)>0){
         Integer integral =(Integer) map.get("payMoney")/10;
         map.put("integral",integral);
         if( businessOrderMapper.updateIntegral(map)>0){
          return resultJson.setSuccess("评论成功,已为您增加积分:"+integral);}
      }}
        return resultJson.setFail("评论失败");
    }

    @Override
    public ResultJson selectFinalPrice(Map<String, Object> map) {
        ResultJson  resultJson=new ResultJson();
        Map map1=businessOrderMapper.selectFinalPrice(map);
        return resultJson.setSuccess("成功",map1);
    }

    @Override
    public ResultJson payFinalPrice(Map<String, Object> map) {
        ResultJson  resultJson=new ResultJson();
        OrderState orderState=new OrderState();
        orderState.setState(14);
        orderState.setBusinessOrderId((Integer) map.get("businessOrderId"));
        if (businessOrderMapper.insertOrderState(orderState)>0){
        return resultJson.setSuccess("支付成功");}
        return resultJson.setFail("支付失败");
    }

    @Override
    public ResultJson cancelOrder(Map<String, Object> map) {
        ResultJson  resultJson=new ResultJson();
        OrderState orderState=new OrderState();
        orderState.setState(-1);
        orderState.setBusinessOrderId((Integer) map.get("businessOrderId"));
        if (businessOrderMapper.insertOrderState(orderState)>0){
            return resultJson.setSuccess("取消成功");}
        return resultJson.setFail("取消失败");
    }

    @Override
    public ResultJson tuikuanOrder(Map<String, Object> map) {
        ResultJson  resultJson=new ResultJson();
        OrderState orderState=new OrderState();
        orderState.setState(0);
        orderState.setBusinessOrderId((Integer) map.get("businessOrderId"));
        if (businessOrderMapper.insertOrderState(orderState)>0){
            return resultJson.setSuccess("已申请，待审核");}
        return resultJson.setFail("申请失败");
    }
    }



